package com.edwarder.action;

import com.alibaba.fastjson.JSON;
import com.edwarder.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: jzhang
 * @Date: 2018/9/21 10:38
 * @Description:
 */
@WebServlet("/helloAjax")
public class HelloAjax extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        System.out.println("name:" + name + ", age:" + age);
        System.out.println("doGet请求成功！");

        User u = new User();
        u.setId(1001);
        u.setName(name);
        u.setAge(Integer.valueOf(age));
        //将对象转字符串传递到前台
        String userString = JSON.toJSONString(u);
        response.getWriter().println(userString);
    }
}
